package com.hikeen.launcherx.utils;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class FloatingDraftButton extends FloatingActionButton implements View.OnTouchListener {

    int lastX, lastY;
    int originX, originY;
    final int screenWidth;
    final int screenHeight;

    public static int viewHight;
    public static int viewWight;

    private ArrayList<FloatingActionButton> floatingActionButtons = new ArrayList<FloatingActionButton>();

    public FloatingDraftButton(Context context) {
        this(context, null);
    }

    public FloatingDraftButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatingDraftButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        screenWidth = AppUtils.getScreenWidth(context);
        screenHeight = AppUtils.getContentHeight(context);
        setOnTouchListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWight = getMeasuredWidth();
        viewHight = getMeasuredHeight();
        Log.d("TAG", "onMeasure: " + viewWight + "  " + viewHight);
    }

    //注册归属的FloatingActionButton
    public void registerButton(FloatingActionButton floatingActionButton) {
        floatingActionButtons.add(floatingActionButton);
    }

    public ArrayList<FloatingActionButton> getButtons() {
        return floatingActionButtons;
    }

    public int getButtonSize() {
        return floatingActionButtons.size();
    }

    //是否可拖拽  一旦展开则不允许拖拽
    public boolean isDraftable() {
        for (FloatingActionButton btn : floatingActionButtons) {
            if (btn.getVisibility() == View.VISIBLE) {
                return false;
            }
        }
        return true;
    }

    //当被拖拽后其所属的FloatingActionButton 也要改变位置
    private void slideButton(int l, int t, int r, int b) {
        for (FloatingActionButton floatingActionButton : floatingActionButtons) {
            floatingActionButton.layout(l, t, r, b);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (!isDraftable()) {
            return false;
        }
        int ea = event.getAction();
        switch (ea) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();// 获取触摸事件触摸位置的原始X坐标
                lastY = (int) event.getRawY();
                originX = lastX;
                originY = lastY;
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;
                int l = v.getLeft() + dx;
                int b = v.getBottom() + dy;
                int r = v.getRight() + dx;
                int t = v.getTop() + dy;
                // 下面判断移动是否超出屏幕
                int bj = 120;//按钮距离屏幕边界的距离,默认为0
                if (l < bj) {
                    l = 0 + bj;
                    r = l + v.getWidth();
                }
                if (t < bj) {
                    t = 0 + bj;
                    b = t + v.getHeight();
                }
                if (r > screenWidth - bj) {
                    r = screenWidth - bj;
                    l = r - v.getWidth();
                }
                if (b > screenHeight - bj) {
                    b = screenHeight - bj;
                    t = b - v.getHeight();
                }
                v.layout(l, t, r, b);
                slideButton(l, t, r, b);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                v.postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                int distance = (int) event.getRawX() - originX + (int) event.getRawY() - originY;
                Log.e("DIstance", distance + "");
                if (Math.abs(distance) < 20) {
                    //当变化太小的时候什么都不做 OnClick执行
                } else {
                    return true;
                }
                break;
        }
        return false;

    }

    public void reflush(Point point) {
        int dx = point.x;
        int dy = point.y;
        int l = dx - viewWight / 2;
        int b = dy + viewHight / 2;
        int r = dx + viewWight / 2;
        int t = dy - viewHight / 2;
        // 下面判断移动是否超出屏幕
//        int bj = 120;//按钮距离屏幕边界的距离,默认为0
//        if (l < bj) {
//            l = 0 + bj;
//            r = l + viewWight;
//        }
//        if (t < bj) {
//            t = 0 + bj;
//            b = t + viewHight;
//        }
//        if (r > screenWidth - bj) {
//            r = screenWidth - bj;
//            l = r - viewWight;
//        }
//        if (b > screenHeight - bj) {
//            b = screenHeight - bj;
//            t = b - viewHight;
//        }
        layout(l, t, r, b);
        slideButton(l, t, r, b);
        lastX = point.x;
        lastY = point.y;
        postInvalidate();
    }

}
